﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace ProjectAutumn.Input
{
    static class InputManager
    {
        internal static string GetDll(InputSystems input)
        {
            string assembly = "ProjectAutumn.InputSystem.";
            switch (input)
            {
                case InputSystems.SDL:
                    assembly += "SDL";
                    break;
                case InputSystems.DirectInput:
                    assembly += "DirectInput";
                    if (Utility.IsX64()) assembly += "X64";
                    break;
                default:
                    throw new ArgumentOutOfRangeException("input");
            }
            return assembly + ".dll";
        }
        public static InputDevice CreateInput(InputSystems input, Game game)
        {
            if (input == InputSystems.Null) return new NullInput(game);
            return Utility.LoadClass(GetDll(input), typeof(InputDevice), game) as InputDevice;
        }
        public static bool Exists(InputSystems input)
        {
            if (input == InputSystems.Null) return true;
            string assembly = GetDll(input);
            if (File.Exists(assembly))
            {
                try
                {
                    Assembly.LoadFile(new FileInfo(assembly).FullName);
                    return true;
                }
                catch (BadImageFormatException)
                {
                    return true;
                }
#if DEBUG
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Debug.WriteLine(e);
                }
#endif
                catch { }
            }
            return false;
        }
    }
}
